package diet.service;

/**
 * Created by zyj on 2018/4/11.
 */
import diet.dao.entity.DietInfoPO;
import diet.dao.entity.DietRecordPO;
import diet.dao.mapper.DietMapper;
import org.springframework.stereotype.Service;
import util.ErrorCode;
import util.ResponseUtil;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;
import java.util.ArrayList;

@Service
public class DietService {

    @Resource
    private DietMapper dietMapper;

    public String selcetDietRecord(Integer userId,Date date){
        System.out.println("in selcetDietRecord");
        List<DietRecordPO> dietRecordPOS=dietMapper.selcetDietRecord(userId,date);
        System.out.println(dietRecordPOS);
        List<DietRecordVO> dietRecordVOS=new ArrayList<DietRecordVO>();
        for(DietRecordPO dietRecordPO: dietRecordPOS){
            DietRecordVO tmpDietRecordVO=new DietRecordVO();
            DietInfoPO tmpDietInfoPO=dietMapper.getDietInfoById(dietRecordPO.getDietId());
            tmpDietRecordVO.setDietInfoPO(tmpDietInfoPO);
            tmpDietRecordVO.setDietRecordPO(dietRecordPO);
//            tmpDietRecordVO.setDietName(tmpDietInfoPO.getName());
//            tmpDietRecordVO.setDietunit(tmpDietInfoPO.getUnit());
//            tmpDietRecordVO.setRecordAmount(dietRecordPO.getAmount());
//            tmpDietRecordVO.setRecordDate(dietRecordPO.getDate());
//            tmpDietRecordVO.setRecordEnergy(dietRecordPO.getEnergy());
//            tmpDietRecordVO.setRecordComment(dietRecordPO.getComment());
            dietRecordVOS.add(tmpDietRecordVO);
        }
        if (!dietRecordVOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", dietRecordVOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "饮食记录为空", dietRecordVOS);
        }

    }

    public String selcetALLDiet(){
        List<DietInfoPO> DietInfoPOS=dietMapper.selcetALLDietInfo();
        if (!DietInfoPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", DietInfoPOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "食物列表为空", DietInfoPOS);
        }
    }

    public String selcetDietById(Integer dietId){
        DietInfoPO dietInfoPO=dietMapper.getDietInfoById(dietId);
        if (dietInfoPO!=null) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", dietInfoPO);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "获取食物信息失败", dietInfoPO);
        }
    }

    public String selcetDietRecordById(Integer dietRecordId){
        DietRecordPO dietRecordPO=dietMapper.getDietRecordById(dietRecordId);
        if (dietRecordPO!=null) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", dietRecordPO);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "获取饮食记录失败", dietRecordPO);
        }
    }

    public String searchDietByNmae(String name){
        System.out.println("searchDietByNmae");
        List<DietInfoPO> DietInfoPOS=dietMapper.selectDietInfoByName(name.trim());
        if (!DietInfoPOS.isEmpty()) {
            return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "successful", DietInfoPOS);
        } else {
            return ResponseUtil.getResponseString(ErrorCode.NOT_EXIST, "食物列表为空", DietInfoPOS);
        }
    }

    public String insertDietRecord(DietRecordVO dietRecordVO){
        System.out.println("in insertDietRecord");
        DietInfoPO tmpDietInfo=dietRecordVO.getDietInfoPO();
        DietRecordPO tmpDietRecord=dietRecordVO.getDietRecordPO();
//        Integer energy=tmpDietInfo.getKcal()*tmpDietRecord.getAmount();
//        String strEnergy=energy+"kcal";
//        tmpDietRecord.setEnergy(strEnergy);
        int userid=tmpDietRecord.getUserId();
        Date date=tmpDietRecord.getDate();
        List<DietRecordPO> dietRecordPOS=dietMapper.selcetDietRecord(userid,date);
        for(DietRecordPO dietRecordPO:dietRecordPOS){
            if(dietRecordPO.getDietId()==tmpDietRecord.getDietId()&&dietRecordPO.getType().equals(tmpDietRecord.getType())){
                System.out.println("insert ,有重复记录");
                DietRecordVO tmpDietRecordVO=new DietRecordVO();
                dietRecordPO.setAmount(dietRecordPO.getAmount()+tmpDietRecord.getAmount());
                dietRecordPO.setEnergy(dietRecordPO.getAmount()*tmpDietInfo.getKcal());
                tmpDietRecordVO.setDietRecordPO(dietRecordPO);
                tmpDietRecordVO.setDietInfoPO(tmpDietInfo);
                System.out.println(tmpDietRecordVO);
                updateDietRecord(tmpDietRecordVO);
                return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "饮食记录添加成功(有重复记录)", tmpDietRecordVO.getDietRecordPO());
            }
        }
        //System.out.println(tmpDietRecord);
        dietMapper.insertDietRecord(tmpDietRecord);
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "饮食记录添加成功", tmpDietRecord);
    }

    public String delectDietRecord(Integer id){
        dietMapper.delectDietRecord(id);
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "删除成功", "");
    }

    public String updateDietRecord(DietRecordVO dietRecordVO){
        int id=dietRecordVO.getDietRecordPO().getId();
        System.out.println("in update: id="+id);
        System.out.println(dietRecordVO);
        delectDietRecord(id);
        insertDietRecord(dietRecordVO);

        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL, "更新饮食记录成功", "");
    }
}
